package com.hinnotek.amoeba.adapters.old

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.luck.picture.lib.config.PictureMimeType
import com.luck.picture.lib.entity.LocalMedia
import java.util.*
import com.hinnotek.amoeba.R

/**
 * @author XUE
 * @since 2019/3/25 11:29
 */
class ImageSelectGridAdapter(context: Context?, onAddPicClickListener: OnAddPicClickListener) :
    RecyclerView.Adapter<ImageSelectGridAdapter.ViewHolder>() {
    private val mInflater: LayoutInflater
    private var mList = mutableListOf<LocalMedia>()
    private var mSelectMax = 9

    /**
     * 点击添加图片跳转
     */
    private val mOnAddPicClickListener: OnAddPicClickListener

    interface OnAddPicClickListener {
        fun onAddPicClick()
    }

    fun setSelectMax(selectMax: Int) {
        mSelectMax = selectMax
    }

    fun setSelectList(list: MutableList<LocalMedia>) {
        mList = list
    }

    fun update(list: MutableList<LocalMedia>) {
        mList = list
        notifyDataSetChanged()
    }

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var ivSelectPic: ImageView
        var llDelete: LinearLayout

        init {
            ivSelectPic = view.findViewById(R.id.iv_select_pic)
            llDelete = view.findViewById(R.id.ll_delete)
        }
    }

    override fun getItemCount(): Int {
        return if (mList.size < mSelectMax) {
            mList.size + 1
        } else {
            mList.size
        }
    }

    override fun getItemViewType(position: Int): Int {
        return if (isShowAddItem(position)) {
            TYPE_CAMERA
        } else {
            TYPE_PICTURE
        }
    }

    /**
     * 创建ViewHolder
     * 创建ViewHolder
     */
    override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): ViewHolder {
        val view: View =
            mInflater.inflate(R.layout.adapter_select_image_grid_item, viewGroup, false)
        return ViewHolder(view)
    }

    private fun isShowAddItem(position: Int): Boolean {
        val size = mList.size
        return position == size
    }

    /**
     * 设置值
     */
    override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
        //少于8张，显示继续添加的图标
        if (getItemViewType(position) == TYPE_CAMERA) {
            viewHolder.ivSelectPic.setImageResource(R.drawable.ic_add_image)
            viewHolder.ivSelectPic.setOnClickListener { v: View? -> mOnAddPicClickListener.onAddPicClick() }
            viewHolder.llDelete.visibility = View.INVISIBLE
        } else {
            viewHolder.llDelete.visibility = View.VISIBLE
            viewHolder.llDelete.setOnClickListener { view: View? ->
                val index = viewHolder.adapterPosition
                // 这里有时会返回-1造成数据下标越界,具体可参考getAdapterPosition()源码，
                // 通过源码分析应该是bindViewHolder()暂未绘制完成导致，知道原因的也可联系我~感谢
                if (index != RecyclerView.NO_POSITION) {
                    mList.removeAt(index)
                    notifyItemRemoved(index)
                    notifyItemRangeChanged(index, mList.size)
                }
            }
            val media = mList[position]
            val mimeType = media.mimeType
            val path: String
            path = if (media.isCut && !media.isCompressed) {
                // 裁剪过
                media.cutPath
            } else if (media.isCompressed || media.isCut && media.isCompressed) {
                // 压缩过,或者裁剪同时压缩过,以最终压缩过图片为准
                media.compressPath
            } else {
                // 原图
                media.path
            }
            if (mimeType == PictureMimeType.ofAudio()) {
                viewHolder.ivSelectPic.setImageResource(R.drawable.pic_audio_placeholder)
            } else {
                val options: RequestOptions = RequestOptions()
                    .centerCrop()
                    .placeholder(R.color.color_f4)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                Glide.with(viewHolder.itemView.context)
                    .load(path)
                    .apply(options)
                    .into(viewHolder.ivSelectPic)
            }
            //itemView 的点击事件
            if (mItemClickListener != null) {
                viewHolder.itemView.setOnClickListener { v: View? ->
                    val adapterPosition = viewHolder.adapterPosition
                    mItemClickListener!!.onItemClick(adapterPosition, v)
                }
            }
        }
    }

    protected var mItemClickListener: OnItemClickListener? = null

    interface OnItemClickListener {
        fun onItemClick(position: Int, v: View?)
    }

    fun setOnItemClickListener(listener: OnItemClickListener?) {
        mItemClickListener = listener
    }

    companion object {
        const val TYPE_CAMERA = 1
        const val TYPE_PICTURE = 2
    }

    init {
        mInflater = LayoutInflater.from(context)
        mOnAddPicClickListener = onAddPicClickListener
    }
}